package com.zheng.study.base.bean.search.rule;

import com.zheng.study.base.bean.search.SearchEnum;
import org.hibernate.type.Type;
import org.springframework.util.Assert;

/**
 * 查询 - 参数 - RuleSql
 * ============================================================================
 * author : fallenpanda
 * createDate:  2018/6/11 。
 * ============================================================================
 */
public class RuleSql extends Rule {

	private final String sql;
	private final Object[] values;
	private final Type[] types;

	public RuleSql(SearchEnum.RuleType ruleType, String sql, Object[] values, Type[] types) {
		super(ruleType);
		this.sql = sql;
		this.values = values;
		this.types = types;
	}

	public String getSql() {
		return sql;
	}

	public Object[] getValues() {
		return values;
	}

	public Type[] getTypes() {
		return types;
	}

	public static RuleSql sql(String sql){
		return newBuilder().setRuleType(SearchEnum.RuleType.sql).setSql(sql).build();
	}

	public static RuleSql sql(String sql, Object[] values, Type[] types) {
		return newBuilder().setRuleType(SearchEnum.RuleType.sql).setSql(sql).setValues(values).setTypes(types).build();
	}

	public static Builder newBuilder(){
		return new Builder();
	}

	public static class Builder extends Rule.Builder<Rule, Builder>{

		private String sql;
		private Object[] values;
		private Type[] types;

		public Builder setSql(String sql) {
			this.sql = sql;
			return this;
		}

		public Builder setValues(Object[] values) {
			this.values = values;
			return this;
		}

		public Builder setTypes(Type[] types) {
			this.types = types;
			return this;
		}

		@Override
		protected Builder getThis() {
			return this;
		}

		@Override
		public RuleSql build() {
			Assert.notNull(ruleType, "ruleType 不能为空");
			Assert.hasText(sql, "sql 不能为空");
			return new RuleSql(ruleType, sql, values, types);
		}
	}

}
